(defun
    Cosign ()

    (setq blc (calcBalance))
    (setq inCount (inputCount))
    (if (or (/= blc 0) (= inCount 0))
        (return-from Cosign 0)
    )
    (setq cosignerAddrs (getCurContractDefParamList "cosignerAddrs"))
    (setq threshold (getCurContractDefParam "threshold"))
    (update threshold (Int threshold))
    (setq pks 0)
    (setq sigs 0)
    (for addr cosignerAddrs                                 
        (if (hasPKByAddr addr)
            (progn
                (setq pk (getPKByAddr addr))
                (setq sig (getSig pk))
                (if (not pks)
                    (update pks (list pk))
                    (update pks (cons pk pks))
                )
                (if (not sigs)
                    (update sigs (list sig))
                    (update sigs (cons sig sigs))
                )                
            )
        )
    )                                   
    (setq data (getCurUnitHashToSign))
    (setq ret (verifyMultiSign pks data sigs threshold))
    (if (not ret)
        (return-from Cosign 0)
    )
    (loop (setq i 0) (< i inCount) 
        (progn 
            (if (hasPrevOutParam i "addr")
                (progn       
                    (setq addr (getPrevOutParam i "addr"))          
                    (setq pk (getPKByAddr addr))
                    (setq sig (getSig pk))
                    (setq ret (verify pk data sig))
                    (if (not ret)
                        (return-from Cosign 0)
                    ) 
                ) 
            )
            (update i (+ i 1))
        )
    )
    (return-from Cosign 1)
)

(setq res (Cosign))

(print "Result of Contract[Cosign]: ")
(println res)